
import type {  request, response, nextFunc } from "../../interface/type"
import type {RequestHandler} from "express"
import { ParsedQs } from 'qs';

import {getResult, getErr} from "./getSendResult"
import { CODE } from "@/utils/const";

export const asyncHandler = <T>( handler: (req: request, res: response) => Promise<T> )=> {
    return async (req: request, res: response, next: nextFunc) => {
        try {
            let result = await handler( req, res )
            res.send( getResult( result ) )
        } catch (error: any) {
            console.log(error.message);
            res.send( getErr(error.message, CODE.errorCode ) )
        }
    }
}